package com.guli.gulimall.gulimallproduct.service.impl;

import com.guli.gulimall.gulimallproduct.vo.AttrVo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.gulimallproduct.dao.ProductAttrValueDao;
import com.guli.gulimall.gulimallproduct.entity.ProductAttrValueEntity;
import com.guli.gulimall.gulimallproduct.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
    @Autowired
    ProductAttrValueService productAttrValueService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveProductAttr(List<ProductAttrValueEntity> productAttrValueEntityList) {
         this.saveBatch(productAttrValueEntityList);
    }

    @Override
    public List<ProductAttrValueEntity> listforspuByspuId(String spuId) {

        return productAttrValueService.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
    }

    @Transactional
    @Override
    public void updateSpuAttr(Long spuid, List<ProductAttrValueEntity> attrs) {
        System.out.println(attrs);
//        //1、删除这个spuid对应的所有属性
        this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuid));
        //1、更新
        List<ProductAttrValueEntity> productAttrValueEntityList = attrs.stream().map(item -> {

            item.setSpuId(spuid);

            return item;
        }).collect(Collectors.toList());
        System.out.println();
        this.saveBatch(productAttrValueEntityList);
    }

}